﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;
using OurFAS.MyCommon;
using System.IO;
using NPOI.HSSF.UserModel;

namespace OurFAS.MyAssetManage
{
    public partial class FrmAssetCount : Form
    {
        private string m_sDataSource = null;

        public FrmAssetCount()
        {
            InitializeComponent();
            InitControls();
        }


        private void InitControls()
        {
            cmbDataSource.SelectedIndex = 0;

            //首先加载所有的数据项
            dgvQuery.DataSource = AssetsHelper.GetBaseAssets();
            InitDataGridTitle();
            InitComboBox();

            //隐藏提示信息
            lblInfo.Visible = false;
        }

        private void InitDataGridTitle()
        {
            int count = dgvQuery.ColumnCount;
            for (int i = 0; i < count; i++)
            {
                if (dgvQuery.Columns[i].HeaderText == "AssetCode")
                {
                    dgvQuery.Columns[i].HeaderText = "资产编号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetTagCode")
                {
                    dgvQuery.Columns[i].HeaderText = "标签号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetName")
                {
                    dgvQuery.Columns[i].HeaderText = "资产名称";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetSpec")
                {
                    dgvQuery.Columns[i].HeaderText = "规格型号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TypeCodeL")
                {
                    dgvQuery.Columns[i].HeaderText = "大类编码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TypeCodeM")
                {
                    dgvQuery.Columns[i].HeaderText = "小类编码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "UseFor")
                {
                    dgvQuery.Columns[i].HeaderText = "资产用途";
                }
                else if (dgvQuery.Columns[i].HeaderText == "InBillCode")
                {
                    dgvQuery.Columns[i].HeaderText = "入库单号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "ProductionFirm")
                {
                    dgvQuery.Columns[i].HeaderText = "生产厂商";
                }
                else if (dgvQuery.Columns[i].HeaderText == "OriCost")
                {
                    dgvQuery.Columns[i].HeaderText = "资产原值";
                }
                else if (dgvQuery.Columns[i].HeaderText == "StartUseDate")
                {
                    dgvQuery.Columns[i].HeaderText = "启用日期";
                }
                else if (dgvQuery.Columns[i].HeaderText == "InCharge")
                {
                    dgvQuery.Columns[i].HeaderText = "负责人";
                }
                else if (dgvQuery.Columns[i].HeaderText == "Dept")
                {
                    dgvQuery.Columns[i].HeaderText = "所在部门";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TaskCode")
                {
                    dgvQuery.Columns[i].HeaderText = "课题段代码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "GetType")
                {
                    dgvQuery.Columns[i].HeaderText = "获取方式";
                }
                else if (dgvQuery.Columns[i].HeaderText == "PurchaseFrom")
                {
                    dgvQuery.Columns[i].HeaderText = "采购来源";
                }
                else if (dgvQuery.Columns[i].HeaderText == "GBCode")
                {
                    dgvQuery.Columns[i].HeaderText = "国标编号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "MeasureInfo")
                {
                    dgvQuery.Columns[i].HeaderText = "计量信息";
                }
                else if (dgvQuery.Columns[i].HeaderText == "Location")
                {
                    dgvQuery.Columns[i].HeaderText = "具体位置";
                }
                else if (dgvQuery.Columns[i].HeaderText == "CertificateNo")
                {
                    dgvQuery.Columns[i].HeaderText = "合格证号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "RepairDate")
                {
                    dgvQuery.Columns[i].HeaderText = "维护日期";
                }
            }
            dgvQuery.Refresh();
        }

        private void InitComboBox()
        {
            cmbAssetName.Items.Clear();
            cmbDept.Items.Clear();
            cmbGetType.Items.Clear();
            cmbMeasureInfo.Items.Clear();
            cmbUseFor.Items.Clear();

            //初始化“资产名称”下拉框
            foreach (Dict_Category record in AssetsHelper.GetCategorys())
            {
                cmbAssetName.Items.Add(record.Category.Trim());
            }

            //初始化“所在部门”下拉框
            foreach (Dict_Dept record in AssetsHelper.GetDepartments())
            {
                cmbDept.Items.Add(record.Dept.Trim());
            }

            //初始化“责任人”下拉框


            //初始化“获取方式”下拉框
            foreach (Dict_GetType record in AssetsHelper.GetGetTypes())
            {
                cmbGetType.Items.Add(record.GetType.Trim());
            }

            //初始化“计量信息”下拉框
            foreach (Dict_MeasureInfo record in AssetsHelper.GetMeasureInfos())
            {
                cmbMeasureInfo.Items.Add(record.MeasureInfo.Trim());
            }

            //初始化“资产用途”下拉框
            foreach (Dict_UseFor record in AssetsHelper.GetUseFors())
            {
                cmbUseFor.Items.Add(record.UseFor.Trim());
            }
        }

        private void chbEnterUseDate_CheckedChanged(object sender, EventArgs e)
        {
            dtpUseDate.Enabled = chbEnterUseDate.Checked;
            cmbUseDate.Enabled = chbEnterUseDate.Checked;

            if (chbEnterUseDate.Checked)
            {
                cmbUseDate.SelectedIndex = 0;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dgvQuery.DataSource = null;

            string dtUseDate = null;
            if (chbEnterUseDate.Checked)
            {
                dtUseDate = DataHelper.GetDateToString(dtpUseDate.Value);
            }

            if (m_sDataSource == "base")
            {
                IList<Tbl_BaseAssets> list = AssetsHelper.GetBaseAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            else if (m_sDataSource == "self")
            {
                IList<Tbl_SelfAssets> list = AssetsHelper.GetSelfAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            else if (m_sDataSource == "measure")
            {
                IList<Tbl_MeasureAssets> list = AssetsHelper.GetMeasureAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            InitDataGridTitle();
        }

        private void cmbDataSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDataSource.SelectedIndex == 0)
            {
                m_sDataSource = "base";
            }
            else if (cmbDataSource.SelectedIndex == 1)
            {
                m_sDataSource = "self";
            }
            else if (cmbDataSource.SelectedIndex == 2)
            {
                m_sDataSource = "measure";
            }
        }

        private void cmbInputDataSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbInputDataSelect.SelectedIndex == 0)
            {
                OpenFileDialog oDlg = new OpenFileDialog();
                oDlg.Filter = "Excel Files (.xls)|*.xls";
                if (oDlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        OurFasDBEntities entity = new OurFasDBEntities();
                        switch (m_sDataSource)
                        {
                            case "base":
                                {
                                    IList<Tbl_BaseAssets> recordList = dgvQuery.DataSource as List<Tbl_BaseAssets>;
                                    FileStream file = new FileStream(oDlg.FileName, FileMode.Open, FileAccess.Read);
                                    HSSFWorkbook workbook = new HSSFWorkbook(file);
                                    HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet;
                                    int rowCount = sheet.LastRowNum;
                                    List<string> tagcodeList = new List<string>();
                                    for (int i = 0; i <= sheet.LastRowNum; i++)
                                    {
                                        HSSFRow row = sheet.GetRow(i) as HSSFRow;
                                        string tagcode = row.GetCell(0).ToString();
                                        tagcodeList.Add(tagcode);
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_BaseAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                            case "self":
                                {
                                    IList<Tbl_SelfAssets> recordList = dgvQuery.DataSource as List<Tbl_SelfAssets>;
                                    FileStream file = new FileStream(oDlg.FileName, FileMode.Open, FileAccess.Read);
                                    HSSFWorkbook workbook = new HSSFWorkbook(file);
                                    HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet;
                                    int rowCount = sheet.LastRowNum;
                                    List<string> tagcodeList = new List<string>();
                                    for (int i = 0; i <= sheet.LastRowNum; i++)
                                    {
                                        HSSFRow row = sheet.GetRow(i) as HSSFRow;
                                        string tagcode = row.GetCell(0).ToString();
                                        tagcodeList.Add(tagcode);
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_SelfAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                            case "measure":
                                {
                                    IList<Tbl_MeasureAssets> recordList = dgvQuery.DataSource as List<Tbl_MeasureAssets>;
                                    FileStream file = new FileStream(oDlg.FileName, FileMode.Open, FileAccess.Read);
                                    HSSFWorkbook workbook = new HSSFWorkbook(file);
                                    HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet;
                                    int rowCount = sheet.LastRowNum;
                                    List<string> tagcodeList = new List<string>();
                                    for (int i = 0; i <= sheet.LastRowNum; i++)
                                    {
                                        HSSFRow row = sheet.GetRow(i) as HSSFRow;
                                        string tagcode = row.GetCell(0).ToString();
                                        tagcodeList.Add(tagcode);
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_MeasureAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                        }
                        tabControl1.SelectedIndex = 1;
                    }
                    catch
                    {
                        MessageBox.Show("请检查输入文件的格式是否正确！");
                        Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                        return;
                    }
                }
            }
            else
            {
                OpenFileDialog oDlg = new OpenFileDialog();
                oDlg.Filter = "Text Files (.txt)|*.txt";
                if (oDlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        OurFasDBEntities entity = new OurFasDBEntities();
                        switch (m_sDataSource)
                        {
                            case "base":
                                {
                                    IList<Tbl_BaseAssets> recordList = dgvQuery.DataSource as List<Tbl_BaseAssets>;
                                    List<string> tagcodeList = new List<string>();
                                    using (System.IO.StreamReader sr = new StreamReader(oDlg.FileName, Encoding.Default))
                                    {

                                        string sLine = "";

                                        while ((sLine = sr.ReadLine()) != null)
                                        {
                                            List<string> sElements = CommonHelper.splitStr(sLine, ',');
                                            if (sElements.Count == 1)
                                            {

                                                tagcodeList.Add(sElements[0]);
                                            }

                                            else
                                            {
                                                MessageBox.Show("请检查输入文件的格式是否正确！");
                                                Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                                                return;
                                            }
                                        }
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_BaseAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                            case "self":
                                {
                                    IList<Tbl_SelfAssets> recordList = dgvQuery.DataSource as List<Tbl_SelfAssets>;
                                    List<string> tagcodeList = new List<string>();
                                    using (System.IO.StreamReader sr = new StreamReader(oDlg.FileName, Encoding.Default))
                                    {

                                        string sLine = "";

                                        while ((sLine = sr.ReadLine()) != null)
                                        {
                                            List<string> sElements = CommonHelper.splitStr(sLine, ',');
                                            if (sElements.Count == 1)
                                            {

                                                tagcodeList.Add(sElements[0]);
                                            }

                                            else
                                            {
                                                MessageBox.Show("请检查输入文件的格式是否正确！");
                                                Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                                                return;
                                            }
                                        }
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_SelfAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                            case "measure":
                                {
                                    IList<Tbl_MeasureAssets> recordList = dgvQuery.DataSource as List<Tbl_MeasureAssets>;
                                    List<string> tagcodeList = new List<string>();
                                    using (System.IO.StreamReader sr = new StreamReader(oDlg.FileName, Encoding.Default))
                                    {

                                        string sLine = "";

                                        while ((sLine = sr.ReadLine()) != null)
                                        {
                                            List<string> sElements = CommonHelper.splitStr(sLine, ',');
                                            if (sElements.Count == 1)
                                            {

                                                tagcodeList.Add(sElements[0]);
                                            }

                                            else
                                            {
                                                MessageBox.Show("请检查输入文件的格式是否正确！");
                                                Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                                                return;
                                            }
                                        }
                                    }
                                    List<AssetCountResult> countResultList = new List<AssetCountResult>();
                                    foreach (Tbl_MeasureAssets record in recordList)
                                    {
                                        if (tagcodeList.Contains(record.AssetTagCode.Trim()))
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "有"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                        else
                                        {
                                            AssetCountResult tempResult = new AssetCountResult()
                                            {
                                                AssetCode = record.AssetCode,
                                                AssetTagCode = record.AssetTagCode,
                                                AssetName = record.AssetName,
                                                CountResult = "无"
                                            };
                                            countResultList.Add(tempResult);
                                        }
                                    }
                                    dgvCountResult.DataSource = null;
                                    dgvCountResult.DataSource = countResultList;
                                }
                                break;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("请检查输入文件的格式是否正确！");
                        Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                        return;
                    }
                }
            }
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
    